$(document).ready(function (){


var index_list_url = "knowledge/internetip/queryInfo";

var index_list_col_header = [
							  {text : "起始IP" ,name : "ipStart",align:"left",width:25},
							  {text : "终止IP" ,name : "ipEnd",align:"left",width:25},
							  {text : "隶属部门或国家" ,name : "countryArea",align:"left",width:25},
							  {text : "部门或国家明细" ,name : "countryAread",align:"left",width:25}
							];

var index_list_col_oper = [
				  			  {icon : "rh-icon rh-edit" ,text : "修改" ,aclick :edit_template_init},
				  			  {icon : "rh-icon rh-delete" ,text : "删除" ,aclick :index_list_delete}
			   			  ];

var list_add_url = "knowledge/internetip/addInfo";

var list_update_url = "knowledge/internetip/updInfo";

var list_delete_url = "knowledge/internetip/doBatchDel";

var search_el;

index_list_get({paramObj:null,isLoad:true,maskObj:"body"});

event_init();

function event_init ()
{
	$("#add_btn").click(function (){
		edit_template_init();
	});

	$("#delete_btn").click(function (){
		index_list_batch_delete();
	});

	$("#refresh_btn").click(function (){
		index_list_get({paramObj:null,isLoad:true,maskObj:"body"});
		if($("#edit_template").size() > 0)
		{
			$("#edit_template").umDataBind("reset");
		}
	});

	$("#search_btn").click(function(){
		if (search_el)
		{
			search_el.parent().show();
			return false;
		}
		search_tpl_init();
	});
}

function index_list_get(option)
{
	g_grid.render($("#table_div") ,{
 		header : index_list_col_header,
 		oper : index_list_col_oper,
 		operWidth : "100px",
 		url : index_list_url,
 		paramObj : option.paramObj,
 		isLoad : option.isLoad,
 		maskObj : option.maskObj,
 		hideSearch:true,
 		dbClick : detail_template_init,
 		hasBorder : false,
	    autoHeight : true,
	    showCount : true
	});
}

function edit_template_init (rowData)
{

	var title = rowData ? "互联网IP地址库修改" : "互联网IP地址库添加";
	$.ajax({
		type: "GET",
		url: "module/base_resource_mant/sec_event_manage/internet_address_tpl.html",
		success : function(data){
			g_dialog.dialog($(data).find("[id=edit_template]") ,{
				width : "600px",
				init : init,
				title : title,
				saveclick : save_click
			});
		}
	});

	function init (el)
	{
		g_validate.init(el);
		var el_startIp = el.find("[data-id=ipStart]");
		var el_endIp = el.find("[data-id=ipEnd]");
		var el_ipv6 = el.find("[data-id=ipv6]");
		el.find("[name=ipRadioStatus]").click(function(){
			if($(this).val() == "0")
			{
				el_startIp.removeAttr("disabled");
				el_endIp.removeAttr("disabled");
				el_ipv6.attr("disabled","disabled");
				el_ipv6.val("");
				g_validate.clear([el_ipv6]);
			}
			else
			{
				el_startIp.attr("disabled","disabled");
				el_endIp.attr("disabled","disabled");
				el_ipv6.removeAttr("disabled");
				el_startIp.val("");
				el_endIp.val("");
				g_validate.clear([el_startIp ,el_endIp]);
			}
		});
		if (rowData) {
			el.umDataBind("render",rowData);
			el.find("[name=ipRadioStatus][value="+rowData.ipRadioStatus+"]").click();
			if (rowData.ipRadioStatus == 1) 
			{
				el.find("[data-id=ipv6]").val(rowData.ipStart);
			}
		}
	}

	function save_click (el ,saveObj)
	{
		if (!g_validate.validate(el))
		{
			return false;
		}

		if (el.find("[name=ipRadioStatus]:checked").val() == 0)
		{
			if (!g_validate.ipValValidate(el.find("[data-id=ipStart]") ,el.find("[data-id=ipEnd]")))
			{
				return false;
			}
		}
		else
		{
			saveObj.ipStart = saveObj.ipv6;
			saveObj.ipEnd = saveObj.ipv6;
		}

		var url = list_add_url;
		if (rowData) 
		{
			url = list_update_url;
			saveObj.ipgId = rowData.ipgId;
		}

		um_ajax_post({
			url : url,
			paramObj : saveObj,
			maskObj:el,
			successCallBack : function (data){
				g_dialog.hide(el);
				g_dialog.operateAlert();
				index_list_get({paramObj:null,isLoad:true,maskObj:"body"});
			}
		});
		
	}
}

function index_list_delete (rowData)
{
	var internetipstore = [];
	internetipstore.push(rowData);
	g_dialog.operateConfirm("确认删除此记录么" ,{
		saveclick : function (){
			um_ajax_post({
				url : list_delete_url,
				paramObj : {internetipstore: internetipstore},
				successCallBack : function(data){
					index_list_get({paramObj:null,isLoad:true,maskObj:"body"});
					g_dialog.operateAlert(null ,"删除成功！");
				}
			});
		}
	});
}

function index_list_batch_delete ()
{
	var internetipstore = g_grid.getData($("#table_div") ,{chk:true})

	if(internetipstore.length == 0){
		g_dialog.operateAlert($("#table_div") ,index_select_one_at_least_msg ,"error");
		return false;
	}

	g_dialog.operateConfirm("确认删除选中的记录么" ,{
		saveclick : function()
		{
			um_ajax_post({
				url : list_delete_url,
				paramObj : {internetipstore : internetipstore},
				successCallBack : function(data){
					index_list_get({paramObj:null,isLoad:true,maskObj:"body"});
					g_dialog.operateAlert(null ,"删除成功！");
				}
			});
		}
	});
}

function detail_template_init (rowData)
{
	$.ajax({
		type: "GET",
		url: "module/base_resource_mant/sec_event_manage/internet_address_tpl.html",
		isLoad:true,
		maskObj:"body",
		success :function(data)
		{
			g_dialog.dialog($(data).find("[id=detail_template]"),{
				width : "500px",
				init : init,
				title : "互联网IP地址库详细信息",
				isDetail : true
			});
		}
	});

	function init (el)
	{
		el.umDataBind("render",rowData);
		if (rowData.ipRadioStatus == 0) 
		{
			el.find("[data-id=ipStart]").text(rowData.ipStart+"-"+rowData.ipEnd);
		}
	}
}

function search_tpl_init()
	{	
		$.ajax({
			type: "GET",
			url: "module/base_resource_mant/sec_event_manage/internet_address_tpl.html",
			success: function(data) {
				g_dialog.dialog($(data).find("[id=edit_template]"), {
					width: "600px",
					title:"互联网IP地址库查询",
					init:init,
					closeType:"hide",
					saveclick: save_click
				});
			}
		});

		function init(el)
		{
			el.find("input").removeAttr("validate","required");
			search_el = el;
			g_validate.init(el);

			var el_startIp = el.find("[data-id=ipStart]");
			var el_endIp = el.find("[data-id=ipEnd]");
			var el_ipv6 = el.find("[data-id=ipv6]");
			el_startIp.attr("validate","ipv4");
			el_endIp.attr("validate","ipv4");
			el_ipv6.attr("validate","ipv6");
			el.find("[name=ipRadioStatus]").click(function(){
				if($(this).val() == "0")
				{
					el_startIp.removeAttr("disabled");
					el_endIp.removeAttr("disabled");
					el_ipv6.attr("disabled","disabled");
					el_ipv6.val("");
					g_validate.clear([el_ipv6]);
				}
				else
				{
					el_startIp.attr("disabled","disabled");
					el_endIp.attr("disabled","disabled");
					el_ipv6.removeAttr("disabled");
					el_startIp.val("");
					el_endIp.val("");
					g_validate.clear([el_startIp ,el_endIp]);
				}
			});
		}

		function save_click(el,saveObj)
		{
			if(!g_validate.validate(el))
			{
				return false;
			}
			if (el.find("[name=ipRadioStatus]:checked").val() == 0)
			{
				if (!g_validate.ipv4Validate(el.find("[data-id=ipStart]") ,el.find("[data-id=ipEnd]")))
				{
					return false;
				}
				if (!g_validate.ipValValidate(el.find("[data-id=ipStart]") ,el.find("[data-id=ipEnd]")))
				{
					return false;
				}
			}
			g_grid.refresh($("#table_div"),{
				queryBefore : function(queryObj){
					return saveObj;
				}
			});
			g_dialog.hide(el, "hide");
		}
	}

});